// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_UI_TYPE } from "../Enum";
import { privacyChannel } from "../Script/privacyChannel";
import { StaticInstance } from "../StaticInstance";
import { formatSeconds } from "../Utils";
import AudioManager from "../manager/AudioManager";
import DataManager from "../manager/DataManager";
import SdkManager from "../manager/SdkManager";
import ToastManager from "../manager/ToastManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BaseLayer extends cc.Component {

    show() {
        this.node.active = true;
    }

    hide() {
        this.node.active = false;
    }

    zoomIn(node: cc.Node, scale: number = 1.5, speed: number = 0.3) {
        node.setScale(scale)
        const act = cc.scaleTo(speed, 1)
        cc.tween(node).then(act).start()
    }

    zoomOut(node: cc.Node, scale: number = 0.5, speed: number = 0.3) {
        node.setScale(scale)
        const act = cc.scaleTo(speed, 1)
        cc.tween(node).then(act).start()
    }

    flip(node: cc.Node) {
        const act1 = cc.scaleTo(0.1, 0, 1)
        const act2 = cc.scaleTo(0.1, 1, 1)
        const act = cc.sequence(act1, act2)
        cc.tween(node).then(act).start()
    }

    // 体力值渲染
    rendorPower() {
        const power = this.node.getChildByName('header_power')
        if (power) {
            const num = cc.find('header_power/num', this.node)
            const btn = cc.find('header_power/btn_plus', this.node)
            num.getComponent(cc.Label).string = `${DataManager.instance.power}/${DataManager.instance.powerMax}`
            switch (privacyChannel.channel) {
                case "ks":
                    btn.getChildByName("icon").active = false
                    if (!btn.hasEventListener('click')) {
                        btn.on('click', () => {
                            AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
                            //StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU, false)
                            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.ADDPOWER)
                        })
                    }

                    break
                default:
                    if (!btn.hasEventListener('click')) {
                        btn.on('click', () => {
                            AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
                            privacyChannel.getChannel().showVideo(() => {

                                AudioManager.instance.playSound(ENUM_AUDIO_CLIP.COLLECT_POWER)
                                DataManager.instance.power += DataManager.instance.powerCollectByVideo
                                DataManager.instance.save()
                                num.getComponent(cc.Label).string = `${DataManager.instance.power}/${DataManager.instance.powerMax}`
                            })
                        })
                    }
                    break
            }

        }
    }


    // 体力计时器
    rendorPowerTimer() {
        const timer = cc.find('header_power/timer', this.node)
        this.unscheduleAllCallbacks()
        let isSchedule: boolean = true
        if (DataManager.instance.power >= DataManager.instance.powerMax) isSchedule = false
        if (DataManager.instance.lastPowerRefreshTime > 0) isSchedule = true
        if (isSchedule) {
            let time = DataManager.instance.powerRefreshTime - DataManager.instance.lastPowerRefreshTime
            timer && (timer.getComponent(cc.Label).string = formatSeconds(time, 'i:s'))
            const callback = () => {
                time -= 1
                if (time <= 0) {
                    DataManager.instance.power += 1
                    this.rendorPower()
                    if (DataManager.instance.power >= DataManager.instance.powerMax) {
                        this.unschedule(callback)
                        timer && (timer.getComponent(cc.Label).string = '')
                        DataManager.instance.lastPowerRefreshTime = 0
                    } else {
                        time = DataManager.instance.powerRefreshTime
                        DataManager.instance.lastPowerRefreshTime = 0
                        timer && (timer.getComponent(cc.Label).string = formatSeconds(time, 'i:s'))
                    }
                } else {
                    timer && (timer.getComponent(cc.Label).string = formatSeconds(time, 'i:s'))
                    DataManager.instance.lastPowerRefreshTime = DataManager.instance.powerRefreshTime - time
                }
                DataManager.instance.lastPowerUpdateTime = new Date().getTime()
                DataManager.instance.save()
            }
            this.schedule(callback, 1)
        } else {
            timer && (timer.getComponent(cc.Label).string = '')
        }
    }
}
